package dog.gui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

import constants.CHANGE;
import containers.Department;
import dog.model.Model;

@SuppressWarnings("serial")
public class DepartmentList extends JPanel implements PropertyChangeListener
	{
	JList list;
	public DepartmentList(Model model)
		{
		model.addPropertyChangeListener(this);
		list = new JList(new DefaultListModel());
		list.setCellRenderer(new DepartmentCellRenderer());
		list.setPrototypeCellValue(new Department(-1,"Aaaaaaaaaaaaaaaaaaaa"));
		Department[] curr = model.gui_getCurrentDepartments();
		for(int i=0; i<curr.length; i++)
			{
			((DefaultListModel)list.getModel()).addElement(curr[i]);
			}
		this.add(new JScrollPane(list));
		}
	@Override
	public void propertyChange(PropertyChangeEvent e)
		{
		String type = e.getPropertyName();
		if (type.equals(CHANGE.DEPARTMENT))
			{	
			Department old = (Department)e.getOldValue();
			Department department = (Department)e.getNewValue();
			DefaultListModel model = (DefaultListModel)list.getModel();
			if (old!=null)
				model.removeElement(old);
			if (department!=null)
				model.addElement(department);
			repaint();
			}
		}
	}
